package com.czxy.servlet;

import cn.itcast.servlet.BaseServlet;
import com.czxy.domain.Student;
import com.czxy.service.StudentService;

import javax.servlet.annotation.WebServlet;
import java.util.List;

@WebServlet("/studentServlet")
public class StudentServlet extends BaseServlet {
    private StudentService studentService = new StudentService();
    public  String  findAll(){
        List<Student> list = studentService.findAll();
        getRequest().setAttribute("studentList",list);
        return "/student/list.jsp";
    }
    public  String findSname() {
        String studentname = getRequest().getParameter("studentname");
        List<Student> list = studentService.findSname(studentname);
        getRequest().setAttribute("studentList", list);
        return "/student/list.jsp";
    }
    public  String add (){

        Student student = toBean(Student.class);

        boolean rel = studentService.add(student);

        if (rel){

            return "redirect:/studentServlet?method=findAll";
        }else {

            getRequest().setAttribute("student",student);
            getRequest().setAttribute("addMag","添加失败,登录名重复");
            return "/student/add.jsp";
        }
    }
    public String delete(){
        String sid = getRequest().getParameter("sid");
        studentService.delete(sid);
        return "redirect:/studentServlet?method=findAll";
    }

   public  String edit(){
       String sid = getRequest().getParameter("sid");

       Student student = studentService.findSId(sid);

       getRequest().setAttribute("student",student);

       return "/student/edit.jsp";
   }

   public  String update(){

       Student student = toBean(Student.class);

       boolean rel = studentService.update(student);

       if (rel){

           return "redirect:/studentServlet?method=findAll";
       }else{

           getRequest().setAttribute("student",student);
           getRequest().setAttribute("updateMsg","编辑失败,登录名重复");

           return "/student/edit.jsp";
       }
   }

   public  String  findGId(){
       String sid = getRequest().getParameter("sid");

       List<Student> list = studentService.findGId(sid);


       getRequest().setAttribute("StudentList",list);

       return "/student/view.jsp";


   }


















}
